package org.brisling.docManage.queryService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.DataTableListDomain;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.LongUtil;
import org.brisling.docManage.domain.DocRead;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
/**
 * 文档阅读回执查询服務service
 * @Author : jackson wang
 * @Date   : 2015年6月8日 下午4:18:01
 * @version: 1.0
 */
@RestController
@RequestMapping("/docread/query")
public class QueryDocReadService extends AbstractBaseQueryService<DocRead>{
	
	
	
	public QueryDocReadService(){
		this.moduleName = ConstSysParam.MODULE_DOCREAD;
		this.subSystem = ConstSysParam.SUBSYSTEM_DOCUMENT;
		this.tableName = this.getTableName(DocRead.class);
		this.clazz = DocRead.class;
		typeMap = this.getTypeMap(clazz);
		
		orderMap.put("modiDate","desc");			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return new ModelAndView("/sysman/sysman_role/main");
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return new ModelAndView("/sysman/sysman_role/editDlg");
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<DocRead> getListDomain(@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap, true);
		
	}
	
	
	
	
	/**
	 * 获取bs datatable 列表数据 
	 * @param body
	 * @return
	 */
	@RequestMapping("/getBsListDomain.action")
	public DataTableListDomain<DocRead> getBsListDomain(@RequestBody(required=false) String body){		
		
		Map<String,Object> map = this.getBodyParam(body);
		
		String _field = "docid";
		if(map.containsKey(_field)){
			Map<String,String> paramMap = (Map<String, String>) map.get(ConstSysParam.CONDITION_MAP_KEY);
			
			if(paramMap==null){
				paramMap = new HashMap<String,String>();
			}
			
			paramMap.put(_field, map.get(_field).toString());
			
			map.put(ConstSysParam.CONDITION_MAP_KEY, map);
		}
		DataTableListDomain<DocRead> domains =list2Table(
					super.getListDomainByConditionAndOrder(null,map,typeMap,orderMap,true,true)
					,getDraw(body));
		
		 
		
		return domains;
		
	}
	/*
	 * 转化domain类型
	 */
	private DataTableListDomain<DocRead> list2Table(ListDomain<DocRead> listDomain,Integer draw){
		
		DataTableListDomain<DocRead> tableDomain = new DataTableListDomain<DocRead>();
		if(listDomain!=null){		
			tableDomain.setRecordsTotal(listDomain.getTotal().intValue());
			tableDomain.setRecordsFiltered(tableDomain.getRecordsTotal());
			tableDomain.setData(listDomain.getRows());
			tableDomain.setDraw(draw);
		}
		return tableDomain;
	}
	
	/*
	 * 获取datatable url请求计数器
	 */
	private Integer getDraw(String body){
		Map<String,Object> conditionMap = this.getBodyParam(body);
		return getDraw(conditionMap);
	}
	
	private Integer getDraw(Map<String,Object> conditionMap){
		if(conditionMap!=null){
			if(conditionMap.containsKey("draw")){
				Object obj = conditionMap.get("draw");
				if(obj!=null){
					return LongUtil.getIntValue(obj.toString(),null);
				}
			}
		}
		return null;
	}
	
	/**
	 * 根据id查找业务信息
	 * @param id	id
	 * @return 业务记录 
	 */
	@RequestMapping("/getOne.action")
	public DocRead getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据业务唯一编码查找记录
	 * @param identify	唯一编码
	 * @return 信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public DocRead getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}	
	
	
	@RequestMapping("/getListByField.action")
	public List<DocRead> getListByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getListByIdentify(field,fieldVal);
	}
}
